﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmProduct : Form
    {
        // new product instance 
        Product product = new Product();

        public delegate void UpdateProductListHandler();

        public UpdateProductListHandler UpdateProductList;

        
        public frmProduct()
        {
            InitializeComponent();
            
            
           
            
        }

        public frmProduct(int ProductId)
        {

            InitializeComponent();

            GetCategories();

            GetProjects();

            GetUnits();

            GetStoreList();

            GetVendorList();
            
            GetProductData(ProductId);
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            frmCategory categoryForm = new frmCategory();

           categoryForm.CategoriesUpdated += new frmCategory.UpdateCategoriesHandler(GetCategories);
            //categoryForm.Parent = this;
            categoryForm.Show();
        }

        /// <summary>
        ///  Binds a categories to the list
        /// </summary>
        private void GetCategories()
        {
            // 
            cboCategories.DataSource = CategoryDataControl.Instance.GetAllCategories();
            cboCategories.DisplayMember = "CategoryName";
        }

        /// <summary>
        /// Binds projects to the list
        /// </summary>
        private void GetProjects()
        {
            cboProject.DataSource = ProjectDataControl.Instance.GetAllProjects();
            cboProject.DisplayMember = "Name";
        }

        private void GetUnits()
        {
            cboUnit.DataSource = UnitOfMeasureDataControl.Instance.GetMeasures();
            cboUnit.DisplayMember = "Name";
        }

        private void GetStoreList()
        {
            cboLocation.DataSource = StorageDataControl.Instance.GetAllStores();
            cboLocation.DisplayMember = "StoreName";
        }

        private void GetVendorList()
        {
            cboVendor.DataSource = VendorDataControl.Instance.GetAllVendors();
            cboVendor.DisplayMember = "VendorName";
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {
            // load the combos 

            GetCategories();

            GetProjects();

            GetUnits();

            GetStoreList();

            GetVendorList();
        }



        private void GetProductData(int productId)
        {

            product = ProductDataControl.Instance.GetById(productId);

            txtProductName.Text = product.ProductName;
            txtDescription.Text = product.ProductDescription;
            cboCategories.SelectedItem = product.Categories;
            cboProject.SelectedItem = product.Project;
            txtManCode.Text = product.ManufacturerCode;
            txtManufacturer.Text = product.Manufacturer;
            txtItemCode.Text = product.BatchNumber;

            if (product.ProductExpires == 1)
            {
                chkExpires.Checked = true;
                dtpExpiryDate.Value = (DateTime) product.ProductExpiry;
            }
           

            txtQuantity.Text = product.ProductQuantity.ToString();
            txtReorderLevel.Text = product.ProductReorderLevel.ToString();
            txtReorderQuantity.Text = product.ProductReorderQuantity.ToString();
            txtPrice.Text =  product.CurrentPrice.ToString();
            cboVendor.SelectedItem = product.Vendor;
            txtSKU.Text = product.Sku;
            txtNotes.Text = product.ProductNotes;
            cboLocation.SelectedItem =  product.Storage;
            cboUnit.SelectedItem = product.UnitOfMeasure;


        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddProject_Click(object sender, EventArgs e)
        {
            frmProjects projects = new frmProjects();

            projects.UpdateProjectList += new frmProjects.UpdateProjectListHandler(GetProjects);
            //projects.Parent = this;
            projects.Show();
        }

        private void btnAddUnit_Click(object sender, EventArgs e)
        {
            frmUnitOfMeasure units = new frmUnitOfMeasure();

            units.UpdateUnitList += new frmUnitOfMeasure.UpdateUnitOfMeasureHandler(GetUnits);
            units.Show();
        }

        private void btnAddStore_Click(object sender, EventArgs e)
        {
            frmStorage store = new frmStorage();
            store.UpdateStoreList += new frmStorage.UpdateStoreListHandler(GetStoreList);
            store.Show();
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            Save();
            UpdateProductList();
        }


        private void Save()
        {

            // get the product object 
            product.ProductName = txtProductName.Text;
            product.ProductDescription = txtDescription.Text;
            product.Categories = GetSelectedCategory();
            product.Project = GetSelectedProject();
            product.ManufacturerCode = txtManCode.Text;
            product.Manufacturer = txtManufacturer.Text;
            product.BatchNumber = txtItemCode.Text;

            if (chkExpires.Checked)
            {
                product.ProductExpires = 1;
                product.ProductExpiry = dtpExpiryDate.Value;
            }
            else
            {
                product.ProductExpires = 0;
            }

            decimal quantity;
            decimal reorder;
            decimal reorderQuantity;
            decimal price;

            decimal.TryParse(txtQuantity.Text, out quantity);
            decimal.TryParse(txtReorderLevel.Text, out reorder);
            decimal.TryParse(txtReorderQuantity.Text, out reorderQuantity);
            decimal.TryParse(txtPrice.Text, out price);

            product.ProductQuantity = quantity;
            product.ProductReorderLevel = reorder;
            product.ProductReorderQuantity = reorderQuantity;
            product.CurrentPrice = price;
            product.Vendor = GetSelectedVendor();
            product.Sku = txtSKU.Text;
            product.ProductNotes = txtNotes.Text;
            product.Storage = GetSelectedStore();
            product.UnitOfMeasure = GetselectedUnit();


            ProductDataControl.Instance.Save(product);
        }

        private UnitOfMeasure GetselectedUnit()
        {
            UnitOfMeasure unit;
            unit = (UnitOfMeasure)cboUnit.SelectedItem;
            return unit;
        }

        private Project GetSelectedProject()
        {
           Project p = new Project();
            p = (Project) cboProject.SelectedItem;
            return p;
        }

        private Storage GetSelectedStore()
        {
            Storage store;

            store = (Storage)cboLocation.SelectedItem;

            return store;
        }

       private Storage GetStoreLocation()
        {
            Storage storage = new Storage();

            storage = (Storage) cboLocation.SelectedItem;

            return storage;
        }

        private Project GetOwningProject()
        {
            Project project = new Project();
            project = (Project) cboProject.SelectedItem;

            return project;
        }

        private Vendor GetSelectedVendor()
        {
            Vendor v = new Vendor();
            v = (Vendor)cboVendor.SelectedItem;

            return v;
        }

        private Categories GetSelectedCategory()
        {
            Categories cat;

            cat = (Categories) cboCategories.SelectedItem;

            return cat;
        }

        private void btnOpenPriceHistory_Click(object sender, EventArgs e)
        {
         
        }

        private void cmdCopy_Click(object sender, EventArgs e)
        {
            product.Productid = 0;
            product.Sku = null;

        }

        

       
    }
}
